package org.tinygroup.trans.xstream.base;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.tinygroup.trans.xstream.manager.XStreamTransManager;
import org.tinygroup.vfs.FileObject;
import org.tinygroup.vfs.VFS;

import com.thoughtworks.xstream.XStream;

public class XStreamSceneMappingManager {
	static Map<String, String> scene_name = new HashMap<String, String>();

	public static String getXStreamPackageName(String scene) {
		return scene_name.get(scene);
	}

	public static void putSceneName(String scene, String xstreamPackageName) {
		scene_name.put(scene, xstreamPackageName);
	}

	public static void removeSceneName(String scene) {
		scene_name.remove(scene);
	}

	public static void initXStreamSceneMapping(String filePath) {
		XStream stream = new XStream();
		stream.autodetectAnnotations(true);
		stream.alias("xstream-scenes", XStreamSceneMappings.class);
		stream.setClassLoader(XStreamTransManager.class.getClassLoader());
		FileObject fileObject = VFS.resolveFile(filePath);
		XStreamSceneMappings mappings = (XStreamSceneMappings) stream
				.fromXML(fileObject.getInputStream());
		VFS.closeInputStream(fileObject);
		List<XStreamSceneMapping> sceneMappings = mappings.getSceneMappings();
		for (XStreamSceneMapping mapping : sceneMappings) {
			putSceneName(mapping.getScene(), mapping.getXstreamPackageName());
		}
	}
}
